//#include "qt1.h"
#include "delegate.h"
#include "dblDele.h"
#include "widgetdelegate.h"
#include <QHeaderView>
#include <QStandardItemModel>
#include <QTableView>
#include "ApplicationWindow.h"
#include <QtGui>
#include <QApplication>
#include "Madlglogon.h"
#include "dbdata.h"
#include "cmysig.h"
#include <mytp.h>
#include "ccard.h"
#include "card9111.h"
#include "mcsy.h"
#include "mcsytimer.h"
#include "mcsythread.h"
#include <QMutex>
//#include "PythonScripting.h"
//#include "ScriptWindow.h"
//PythonScripting * s;
#include "Mylog.h"
#include "channelad.h"
// #include "mySerial.h"
// #include "main_curve.h"
//QList<ChannelAD> *pgchannels;
QStringList *pmychannelNames;
QHash<QString, ChannelAD> *pgchannels;
CDBdata * pgdb;
MUser * puser1;
CCard *pcard;
MCsyThread * pcsy;
MyLOG * plog;
//QextSerialPort *pmyport;
MyTp * ptp;
// MySerial * pMySerial;
QMutex   mutex;
FILE *output=NULL;
void testQByteArray()
{
    float f=0.12;
    char * pchar;
    pchar=(char *)&f;
    QByteArray b;
    b=QByteArray::fromRawData(pchar,4);
    //float f2=0;

    //b.QByteRef
}
void testSig()
{
    CMySig sig1;
    sig1.testData();
//    sig1.writeTextFile("test.txt");
//    sig1.readFile("test.txt");
//    QStringList l=sig1.toStringList();
//    qDebug()<<l;
   // sig1.readTextFile("new01.txt");
    // sig1.writeTextFile("test2.txt");
    // sig1.saveSig(1);
}
void testSigClone()
{
    CMySig sig1;
    sig1.testData();
    sig1.writeTextFile("test.txt");
    CMySig sig2;
    // qDebug()<<sig1.getXn();
    sig2.clone(&sig1);
    sig2.writeTextFile("sig2.txt");
}
void testColor(int c)
{
    qDebug()<<c;
    QRgb r(c);
    qDebug()<<r;
    QColor color(r);
    qDebug()<<color;
}
void testDb(){
    
}
void redirectOutput(QtMsgType type,const QMessageLogContext & c,const QString &msg)
{
    QByteArray localMsg=msg.toLocal8Bit();
    fprintf(output, "%s\n",localMsg.constData());
    fflush(output);
}
int main(int argc, char *argv[])
{
    output=stdout;
    qInstallMessageHandler(redirectOutput);

    QApplication app(argc, argv);
    QObject::connect(&app, SIGNAL(lastWindowClosed()), &app, SLOT(quit()));
    qDebug()<<"begin";
    printf("begin");

    //testQByteArray();return 0;
    //
    qRegisterMetaType<CMySig>("CMySig");
    //qRegisterMetaType("QList<double>");
    qRegisterMetaType<QList<double> >("QList<double>");


    CDBdata gdb;
    pgdb=&gdb;
    //
    QHash<QString, ChannelAD> gchannels;
    pgchannels=&gchannels;
    pgdb->getChannels(pgchannels);

    QStringList channelNames;
    pmychannelNames=&channelNames;
    channelNames.append("C");
    channelNames.append("S");
    channelNames.append("C2");
    channelNames.append("S2");

//    int n=channelNames.size();
//    for (int i=0;i<n;i++)
//    {
//        testColor((*pgchannels)[channelNames[i]].color);
//    }
//    return 0;
    //pgchannels->
    //qDebug()<<pgdb->nextId();return 0;
   // testSigClone();return 0;
//    pgdb->loadSig(1);return 0;
    MUser user1;
    puser1=&user1;

    MyTp tp1;
    ptp=&tp1;
    // MySerial myserial1;
    // pMySerial=&myserial1;

    Card9111 card1;
    pcard=&card1;

    MCsyThread csy1;
    csy1.setCard(pcard);
    pcsy=&csy1;

    MyLOG  log1;
    plog=&log1;
    plog->logstr("begin");
    int r=0;
    // Madlglogon l;
    // l.show();
    
    // if(QDialog::Accepted==l.exec())
    // {
       ApplicationWindow w;
       w.show();

       //PythonScripting s1(&w);
       //ScriptWindow w1(&s1,&w);
       //w1.show();
       r=app.exec();
    // }
    // else
    // {
    //     r=-1;
    // }
    plog->logstr("end");
    return r;
}
